﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Concurrency;
using PicturePortfolio.ServiceReference;
using PicturePortfolio.Silverlight.Message;
using Caliburn.Micro;
namespace PicturePortfolio.Silverlight.ViewModels
{
    public class ThumbnailViewModel:PropertyChangedBase
    {
        PhotoServiceClient _service;
        public ThumbnailViewModel(PhotoServiceClient photoService)
        {
            _service = photoService;
            this.ThumbNails = new ObservableCollection<IndividualThumbnailViewModel>();
            this.GetAllThumbnails();
        }

        public ThumbnailViewModel(PhotoServiceClient photoService, Category filterCategory)
        {
            _service = photoService;
            this.ThumbNails = new ObservableCollection<IndividualThumbnailViewModel>();
            this.GetThumbnailsByCategory(filterCategory);
        }
        public void Refresh()
        {
            
            try
            {
                this.ThumbNails = new ObservableCollection<IndividualThumbnailViewModel>();
                //this.ThumbNails.Clear();
                NotifyOfPropertyChange(() => ThumbNails);
                //this.ThumbNails = new ObservableCollection<IndividualThumbnailViewModel>();
                this.GetAllThumbnails();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private ObservableCollection<IndividualThumbnailViewModel> _thumbnails;
        public ObservableCollection<IndividualThumbnailViewModel> ThumbNails
        {
            get { return _thumbnails; }
            private set
            {
                _thumbnails = value;
                //NotifyOfPropertyChange(() => ThumbNails);
            }
        }

        private void GetAllThumbnails()
        {
            var func=Observable.FromEvent<GetAllPhotosCompletedEventArgs>(_service,"GetAllPhotosCompleted")
                .ObserveOn(Scheduler.ThreadPool);
            _service.GetAllPhotosAsync();
            func.ObserveOn(Scheduler.Dispatcher).Select(result => result.EventArgs.Result).Subscribe(s => this.ParseThumbnails(s.ToList()));
        }

        private void GetThumbnailsByCategory(Category category)
        {
            var func = Observable.FromEvent<GetPhotosByCategoryCompletedEventArgs>(_service, "GetPhotosByCategoryCompleted")
                .ObserveOn(Scheduler.ThreadPool);
            _service.GetPhotosByCategoryAsync(category);
            func.ObserveOn(Scheduler.Dispatcher)
                .Select(result => result.EventArgs.Result)
                .Subscribe(s => this.ParseThumbnails(s.ToList()));
        }

        private void ParseThumbnails(List<Photo> photos)
        {
            foreach (Photo photo in photos)
            {
                IndividualThumbnailViewModel individualThumbNail = new IndividualThumbnailViewModel(photo);
                this._thumbnails.Add(individualThumbNail);
            }
            NotifyOfPropertyChange(() => ThumbNails);
        }

        public void ImageSelectionChanged(object selectedItem)
        {
            IndividualThumbnailViewModel individualThumb = (IndividualThumbnailViewModel)selectedItem;
            if (individualThumb == null)
                return;
            if (individualThumb.Photo != null)
            {
                ImageSelected imgSelected = new ImageSelected(individualThumb.Photo);
                EventAggregationProvider.EventAggregator.Publish<ImageSelected>(imgSelected);
            }
        }
    }
}
